﻿namespace BlackHole
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using BlackHole.DAO;
    using NPOI.HSSF.UserModel;

    public partial class Overview : System.Web.UI.Page
    {
        public override void VerifyRenderingInServerForm(Control control)
        {
            // should be reserved
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            // Utility.GetRepairRecords(1,2,"2011-8-25","2011-10-25",3);
            if (!IsPostBack)
            {
                RenderUI();
            }
        }

        protected void ExportProductDetails()
        {
        }

        // Export to Excel from a GridView
        protected void ExportToExcel()
        {
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("content-disposition", "attachment;filename=MyFiles.xls");
            Response.Charset = string.Empty;
            this.EnableViewState = false;

            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

            dataRecords.RenderControl(htw);

            Response.Write(sw.ToString());
            Response.End();
        }

        protected void ExportToExcel(MemoryStream ms, string fileName)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("content-disposition", "attachment;filename=" + fileName + ".xls");
            Response.Charset = string.Empty;
            this.EnableViewState = false;

            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
            Response.OutputStream.Write(ms.ToArray(), 0, ms.ToArray().Length);
            Response.End();
        }

        protected void RenderUI()
        {
            comboDistricts.Items.Add(new ListItem("(全部)", "0"));
            foreach (District d in Utility.GetAllDistricts())
            {
                comboDistricts.Items.Add(new ListItem(d.DistrictName, d.Id.ToString()));
            }

            comboDevices.Items.Add(new ListItem("(全部)", "0"));
            foreach (Device d in Utility.GetAllDevices())
            {
                comboDevices.Items.Add(new ListItem(d.DeviceName, d.Id.ToString()));
            }
        }

        protected void comboDistricts_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboOwners.Items.Clear();
            comboOwners.Items.Add(new ListItem("(全部)", "0"));
            foreach (Owner o in Utility.GetOwnersByDistrictId(int.Parse(comboDistricts.SelectedValue)))
            {
                comboOwners.Items.Add(new ListItem(o.OwnerName, o.Id.ToString()));
            }
        }

        protected void comboDevices_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboFaults.Items.Clear();
            comboFaults.Items.Add(new ListItem("(全部)", "0"));
            foreach (Fault f in Utility.GetFaultsByDeviceId(int.Parse(comboDevices.SelectedValue)))
            {
                comboFaults.Items.Add(new ListItem(f.FaultCode + f.FaultName, f.Id.ToString()));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            ExportToExcel();
        }

        protected void btnExportProductDetails_Click(object sender, EventArgs e)
        {
            List<Product> products = Utility.GetAllProductsDetails();
            HSSFWorkbook workbook = new HSSFWorkbook();
            var sheet = workbook.CreateSheet("自古枪兵幸运E");
            sheet.CreateRow(0);

            sheet.GetRow(0).CreateCell(0).SetCellValue("设备名称");
            sheet.GetRow(0).CreateCell(1).SetCellValue("区县");
            sheet.GetRow(0).CreateCell(2).SetCellValue("网点");
            sheet.GetRow(0).CreateCell(3).SetCellValue("状态");
            sheet.GetRow(0).CreateCell(4).SetCellValue("序列号");
            sheet.GetRow(0).CreateCell(5).SetCellValue("Psam号");
            sheet.GetRow(0).CreateCell(6).SetCellValue("固件版本");
            sheet.GetRow(0).CreateCell(7).SetCellValue("维修次数");

            int i = 1;
            foreach (var p in products)
            {
                sheet.CreateRow(i);
                sheet.GetRow(i).CreateCell(0).SetCellValue(p.ProductDeviceName);
                sheet.GetRow(i).CreateCell(1).SetCellValue(p.ProductDistrictName);
                sheet.GetRow(i).CreateCell(2).SetCellValue(p.ProductOwnerName);
                sheet.GetRow(i).CreateCell(3).SetCellValue(p.StatusName);
                sheet.GetRow(i).CreateCell(4).SetCellValue(p.SerialNO);
                sheet.GetRow(i).CreateCell(5).SetCellValue(p.PsamNO);
                sheet.GetRow(i).CreateCell(6).SetCellValue(p.RomVersion);
                sheet.GetRow(i).CreateCell(7).SetCellValue(p.RepairCycle.ToString());
                i++;
            }

            MemoryStream ms = new MemoryStream();
            workbook.Write(ms);
            ExportToExcel(ms, "products");
            ms.Close();
        }

        protected void btnExportProductRecords_Click(object sender, EventArgs e)
        {
            List<RepairRecord> records = Utility.GetRepairRecords(0, 0, 0, 0, "1970/1/1 0:00:00", "2099/1/1 0:00:00");
            HSSFWorkbook workbook = new HSSFWorkbook();
            var sheet = workbook.CreateSheet("yooooooooooooooooooooooo");
            sheet.CreateRow(0);

            sheet.GetRow(0).CreateCell(0).SetCellValue("区县");
            sheet.GetRow(0).CreateCell(1).SetCellValue("网点");
            sheet.GetRow(0).CreateCell(2).SetCellValue("状态");
            sheet.GetRow(0).CreateCell(3).SetCellValue("序列号");
            sheet.GetRow(0).CreateCell(4).SetCellValue("PSAM卡号");
            sheet.GetRow(0).CreateCell(5).SetCellValue("固件版本");
            sheet.GetRow(0).CreateCell(6).SetCellValue("设备类型");
            sheet.GetRow(0).CreateCell(7).SetCellValue("登记故障");
            sheet.GetRow(0).CreateCell(8).SetCellValue("实际故障");
            sheet.GetRow(0).CreateCell(9).SetCellValue("登记日期");
            sheet.GetRow(0).CreateCell(10).SetCellValue("出库时间");
            sheet.GetRow(0).CreateCell(11).SetCellValue("维修方法");
            sheet.GetRow(0).CreateCell(12).SetCellValue("维修备件");

            int i = 1;
            foreach (var r in records)
            {
                sheet.CreateRow(i);
                sheet.GetRow(i).CreateCell(0).SetCellValue(r.DistrictName);
                sheet.GetRow(i).CreateCell(1).SetCellValue(r.OwnerName);
                sheet.GetRow(i).CreateCell(2).SetCellValue(r.ProductStatus);
                sheet.GetRow(i).CreateCell(3).SetCellValue(r.ProductSerialNO);
                sheet.GetRow(i).CreateCell(4).SetCellValue(r.ProductPsamNO);
                sheet.GetRow(i).CreateCell(5).SetCellValue(r.ProductRomVersion);
                sheet.GetRow(i).CreateCell(6).SetCellValue(r.ProductDeviceName);
                sheet.GetRow(i).CreateCell(7).SetCellValue(r.RequestFaultName);
                sheet.GetRow(i).CreateCell(8).SetCellValue(r.RealFaultName);
                sheet.GetRow(i).CreateCell(9).SetCellValue(r.InDate);
                sheet.GetRow(i).CreateCell(10).SetCellValue(r.OutDate);
                sheet.GetRow(i).CreateCell(11).SetCellValue(r.RepairMethod);
                sheet.GetRow(i).CreateCell(12).SetCellValue(r.RepairGear);
                i++;
            }

            MemoryStream ms = new MemoryStream();
            workbook.Write(ms);
            ExportToExcel(ms, "records");
            ms.Close();
        }
    }
}